# @File    : rest_order.py
# @Project : web_learn
import time
from datetime import datetime

from common.commonUtil import CommonUtil
from common.localUtil import LocalUtil
from common.loginUtil import LoginUtil
from common.yamlUtil import YamlUtil
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains


class RestOrder:

    def __init__(self):
        self.conf = YamlUtil("local.yaml").read_yaml()['rest_order']
        self.eip_url = YamlUtil("local.yaml").read_yaml()

    # 半天调休提单方法
    def half_of_put_order(self):
        # 数据驱动获取数据
        data = YamlUtil("data.yaml").read_yaml()

        # 通用方法实例化
        com = CommonUtil()
        # 调用打开网页方法
        wd = com.open_web()
        # 实例化定位封装方法
        local = LocalUtil(wd)
        # 特殊操作实例化
        ac = ActionChains(wd)

        # 调用登录通用类登录方法
        LoginUtil(wd).login_eip()

        # 点击调休申请
        local.wait_for_element(By.XPATH, f"{self.conf['title']}").click()
        time.sleep(4)

        # wd.switch_to.frame(f"{self.conf['frame_id']}")
        # # 切到最外层页面iframe
        wd.switch_to.default_content()
        order_iframe = local.wait_for_elements(By.TAG_NAME, "iframe")
        # print(order_iframe[1].get_attribute('outerHTML'))

        wd.switch_to.frame(order_iframe[1])
        order_iframe_1 = local.wait_for_elements(By.TAG_NAME, "iframe")
        # 切换到调休申请iframe
        wd.switch_to.frame(order_iframe_1[1])
        time.sleep(1)

        # 点击新增
        local.wait_for_element(By.XPATH, f"{self.conf['new_order']}").click()
        time.sleep(5)

        """切换到调休单iframe"""
        # 先切换到最外层iframe
        wd.switch_to.default_content()
        order_iframe_2 = local.wait_for_elements(By.TAG_NAME, "iframe")
        wd.switch_to.frame(order_iframe_2[1])
        order_iframe_3 = local.wait_for_elements(By.TAG_NAME, "iframe")
        # print(order_iframe_3[2].get_attribute('outerHTML'))
        wd.switch_to.frame(order_iframe_3[2])

        """选择加班上班时间"""
        # 点击加班上班时间日历图标
        local.wait_for_element(By.XPATH, f"{self.conf['overtime_start_time']}").click()
        time.sleep(1)

        # 解析时间字符串
        overtime_start_dt = datetime.strptime(data['overtime_start_time'], "%Y-%m-%d %H:%M")
        # 调用通用方法中选择年月方法选中正确年月
        com.select_year_month_component(wd, overtime_start_dt.year, overtime_start_dt.month)
        # 选择日期
        overtime_start_day = local.wait_for_element(By.XPATH,
                                                    f"//td[@lay-ymd='{overtime_start_dt.year}-{overtime_start_dt.month}-{overtime_start_dt.day}']")
        overtime_start_day.click()
        time.sleep(1)

        # 选择时间
        local.wait_for_element(By.XPATH, f"{self.conf['select_time']}").click()
        time.sleep(1)
        # 选择小时
        com.select_time_component(wd, local, "时", str(overtime_start_dt.hour))
        # 选择分钟
        com.select_time_component(wd, local, "分", str(overtime_start_dt.minute))
        # 点击确定
        local.wait_for_element(By.XPATH, f"{self.conf['confirm_time']}").click()
        time.sleep(1)

        """选择加班下班时间"""
        # 点击加班下班时间日历图标
        local.wait_for_element(By.XPATH, f"{self.conf['overtime_end_time']}").click()
        time.sleep(1)

        # 解析时间字符串
        overtime_end_dt = datetime.strptime(data['overtime_end_time'], "%Y-%m-%d %H:%M")
        # 调用通用方法中选择年月方法选中正确年月
        com.select_year_month_component(wd, overtime_end_dt.year, overtime_end_dt.month)
        # 选择日期
        overtime_end_day = local.wait_for_element(By.XPATH,
                                                  f"//td[@lay-ymd='{overtime_end_dt.year}-{overtime_end_dt.month}-{overtime_end_dt.day}']")
        overtime_end_day.click()
        time.sleep(1)

        # 选择时间
        local.wait_for_element(By.XPATH, f"{self.conf['select_time']}").click()
        time.sleep(1)
        # 选择小时
        com.select_time_component(wd, local, "时", str(overtime_end_dt.hour))
        # 选择分钟
        com.select_time_component(wd, local, "分", str(overtime_end_dt.minute))
        # 点击确定
        local.wait_for_element(By.XPATH, f"{self.conf['confirm_time']}").click()
        time.sleep(1)

        """选择调休上班时间"""
        # 点击调休上班时间日历图标
        local.wait_for_element(By.XPATH, f"{self.conf['rest_start_time']}").click()
        time.sleep(1)

        # 解析时间字符串
        rest_start_dt = datetime.strptime(data['rest_start_time'], "%Y-%m-%d %H:%M")
        # 调用通用方法中选择年月方法选中正确年月
        com.select_year_month_component(wd, rest_start_dt.year, rest_start_dt.month)
        # 选择日期
        rest_start_day = local.wait_for_element(By.XPATH,
                                                f"//td[@lay-ymd='{rest_start_dt.year}-{rest_start_dt.month}-{rest_start_dt.day}']")
        rest_start_day.click()
        time.sleep(1)

        # 选择时间
        local.wait_for_element(By.XPATH, f"{self.conf['select_time']}").click()
        time.sleep(1)
        # 选择小时
        com.select_time_component(wd, local, "时", str(rest_start_dt.hour))
        # 选择分钟
        com.select_time_component(wd, local, "分", str(rest_start_dt.minute))
        # 点击确定
        local.wait_for_element(By.XPATH, f"{self.conf['confirm_time']}").click()
        time.sleep(1)

        """选择调休下班时间"""
        # 点击调休下班时间日历图标
        local.wait_for_element(By.XPATH, f"{self.conf['rest_end_time']}").click()
        time.sleep(1)

        # 解析时间字符串
        rest_end_dt = datetime.strptime(data['rest_end_time'], "%Y-%m-%d %H:%M")
        # 调用通用方法中选择年月方法选中正确年月
        com.select_year_month_component(wd, rest_end_dt.year, rest_end_dt.month)
        # 选择日期
        rest_end_day = local.wait_for_element(By.XPATH,
                                              f"//td[@lay-ymd='{rest_end_dt.year}-{rest_end_dt.month}-{rest_end_dt.day}']")
        rest_end_day.click()
        time.sleep(1)

        # 选择时间
        local.wait_for_element(By.XPATH, f"{self.conf['select_time']}").click()
        time.sleep(1)
        # 选择小时
        com.select_time_component(wd, local, "时", str(rest_end_dt.hour))
        # 选择分钟
        com.select_time_component(wd, local, "分", str(rest_end_dt.minute))
        # 点击确定
        local.wait_for_element(By.XPATH, f"{self.conf['confirm_time']}").click()
        time.sleep(2)

        # 点击保存
        local.wait_for_element(By.XPATH, f"{self.conf['save_order']}").click()

    # 调休一天提单方法
    def one_day_put_order(self):
        pass
